﻿using bigscreen_ai_demo.DB;
using Newtonsoft.Json.Linq;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace bigscreen_ai_demo
{

    /// <summary>
    /// MyInput.xaml 的交互逻辑
    /// </summary>
    public partial class MyInput : UserControl
    {

        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(MyInput), new FrameworkPropertyMetadata(String.Empty, (o,e) =>
            {
                //var mi = ((MyInput)o);
                //if (mi.Value_OUTPUT == string.Empty)
                //{
                //    mi.Value_OUTPUT = mi.Value;
                //}

            }));

        /// <summary>
        /// 原始数据
        /// </summary>
        public string Value_OUTPUT
        {
            get { return (string)GetValue(Value_OUTPUTProperty); }
            set { SetValue(Value_OUTPUTProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value_OUTPUT.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Value_OUTPUTProperty =
            DependencyProperty.Register("Value_OUTPUT", typeof(string), typeof(MyInput), new PropertyMetadata(String.Empty));

        /// <summary>
        /// 原始数据
        /// </summary>
        public string Value_SXAI
        {
            get { return (string)GetValue(Value_SXAIProperty); }
            set { SetValue(Value_SXAIProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value_OUTPUT.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Value_SXAIProperty =
            DependencyProperty.Register("Value_SXAI", typeof(string), typeof(MyInput), new PropertyMetadata(String.Empty));



        public string VName
        {
            get { return (string)GetValue(VNameProperty); }
            set { SetValue(VNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VNameProperty =
            DependencyProperty.Register("VName", typeof(string), typeof(MyInput), new PropertyMetadata(""));
        public Boolean IsOUTPUT
        {
            get { return (Boolean)GetValue(IsOUTPUTProperty); }
            set { SetValue(IsOUTPUTProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsOUTPUT.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsOUTPUTProperty =
            DependencyProperty.Register("IsOUTPUT", typeof(Boolean), typeof(MyInput), new PropertyMetadata(false));


        public static readonly RoutedEvent InputChangedEvent = EventManager.RegisterRoutedEvent("InputChanged",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyInput));




        public DateTime OldUpdated
        {
            get { return (DateTime)GetValue(OldUpdatedProperty); }
            set { SetValue(OldUpdatedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OldUpdate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OldUpdatedProperty =
            DependencyProperty.Register("OldUpdated", typeof(DateTime), typeof(MyInput), new FrameworkPropertyMetadata(DateTime.MinValue, FrameworkPropertyMetadataOptions.AffectsRender, 
            (o, e) => {
                // 强制更新绑定
                var control = o as MyInput;
                if (control != null)
                {
                    // 触发绑定更新
                    BindingOperations.GetBindingExpression(control.tt, TextBlock.VisibilityProperty)?.UpdateTarget();
                }
            }));



        public DateTime Updated
        {
            get { return (DateTime)GetValue(UpdatedProperty); }
            set { SetValue(UpdatedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Updated.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UpdatedProperty =
            DependencyProperty.Register("Updated", typeof(DateTime), typeof(MyInput), new FrameworkPropertyMetadata(DateTime.MinValue, FrameworkPropertyMetadataOptions.AffectsRender, 
            (o, e) => {
                // 强制更新绑定
                var control = o as MyInput;
                if (control != null)
                {
                    // 触发绑定更新
                    BindingOperations.GetBindingExpression(control.tt, TextBlock.VisibilityProperty)?.UpdateTarget();
                }
            }));






        public event RoutedEventHandler InputChanged
        {
            add
            {
                AddHandler(InputChangedEvent, value);
            }
            remove
            {
                RemoveHandler(InputChangedEvent, value);
            }
        }

        public MyInput()
        {
            InitializeComponent();
            if (!Config.SXAI_Allow)
            {
                SXAI_Text.Visibility = Visibility.Collapsed;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(Value))
            {
                RoutedEventArgs routedEventArgs = new(routedEvent: InputChangedEvent);
                RaiseEvent(routedEventArgs);
            }

        }
    }


    public class NewValueShowConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var myinput = (MyInput)value;
            if((myinput.Value != myinput.Value_OUTPUT) || (myinput.Updated > myinput.OldUpdated)) {
                return Visibility.Visible;
            }else { 
                return Visibility.Collapsed;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return parameter;
        }
    }


}
